package com.wxpay.config;

import java.util.Map;

import com.wxpay.util.WXPayUtil;

public class WxPayApi {
	//统一下单接口

	private static final String UNIFIEDORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	
	//沙箱统一下单接口
	private static final String UNIFIEDORDER_SANDBOXNEW_URL = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder";
	
	public static final String FIELD_SIGN = "sign";
    public static final String FIELD_SIGN_TYPE = "sign_type";
	
	/**
	 * 统一下单
	 * 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/native_sl.php?chapter=9_1
	 * 商户模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
	 * @param isSandbox 是否是沙盒环境
	 * @param params
	 * @return {String}
	 */
	public static String pushOrder(boolean isSandbox,Map<String, String> params){
		if(isSandbox)
			return doPost(UNIFIEDORDER_SANDBOXNEW_URL, params);
		return doPost(UNIFIEDORDER_URL, params);
	}
	
	public static String doPost(String url, Map<String, String> params){
		try {
			return WXPayUtil.post(url, WXPayUtil.mapToXml(params));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 *枚举  交易类型
	 */
	public static enum TradeType {
		JSAPI,NATIVE,APP,WAP,MICROPAY,MWEB
	}
	
	public static enum SignType {
		MD5, HMACSHA256
	}
}
